<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Tag class
 *
 * @category	Admin
 */
class TagAction extends AdminAction
{

	protected $Tag;

	//初始化
	public function _initialize()
	{
		parent::_initialize();
		$this->Tag = D('Tag');
	}


	//新增tag
	public function addTag()
	{
	    $this->editTag();
	}


	//编辑tag
    public function editTag()
    {
        $tagID = intval($_REQUEST['tag_id']);
        $tagInfo = $this->Tag->find($tagID);
        if ($this->isPost())
        {
            $this->Tag->create();
            if ($_POST['name'] != $tagInfo['name'] && $this->Tag->getTagByName($_POST['name']))
            {
            	$this->error(L('tag.tag_already_exists'));
            }
            $this->Tag->save();
            $this->success(L('tag.tag_edited_succeed'));
        }
        else
        {
            $tagStatusRadios = array(0 => L('locked'), 1 => L('open'));

            $this->assign('tagStatusRadios', $tagStatusRadios);
            $this->assign('tagStatusChecked', $tagInfo['status']);

            $this->assign('rs', $tagInfo);
            $this->display('editTag');
        }
    }


    //tag搜索
    public function searchTag()
    {
        $this->display();
    }


	//tag列表
	public function tagList()
	{
	    import('ORG.Util.Page');
		$where = array();
		$parameter = '';

		if (isset($_GET['name'])) {
			$where['name'] = array('like', '%'.$_GET['name'].'%');
			$parameter .= 'name='.$_GET['name'].'&';
		}
		if (isset($_GET['status']) && 'all' != $_GET['status']) {
			$where['status'] = $_GET['status'];
			$parameter .= 'status='.$_GET['status'].'&';
		}
		if (!empty($_GET['question_count'])) {
			$where['question_count'] = array($_GET['question_count_select'], intval($_GET['question_count']));
			$parameter .= 'question_count_select='.$_GET['question_count'].'&';
			$parameter .= 'question_count='.$_GET['question_count'].'&';
		}

		$count = $this->Tag->where($where)->count();
		$Page = new Page($count);
		$Page->parameter = $parameter;
		$pagination = $Page->show();
		$tagList = $this->Tag->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();

		$this->assign('pagination', $pagination);
		$this->assign('tagList', $tagList);
		$this->display();
	}


	//tag管理
	public function manageTag()
	{
		if ($this->isPost())
		{
			$mods = $_POST['mods'];
			if (empty($mods))
			{
				$this->error(L('tag.no_tag_selected'));
			} else
			{
			    if (isset($_POST['action']['lock']))
			    {
			    	$this->Tag->lockTagByID($mods);
			    }
			    elseif (isset($_POST['action']['open']))
			    {
			        $this->Tag->openTagByID($mods);
			    }
			    elseif (isset($_POST['action']['delete']))
			    {
			        $this->Tag->deleteTagByID($mods);
				}
				$this->success(L('tag.tag_updated_succeed'));
			}
		}
	}


	//同步tag
	function syncTag()
	{
	    $order = array('tag_id' => 'asc');
		$start = isset($_REQUEST['start']) ? intval($_REQUEST['start']) : 0;
		if (empty($start))
		{
			$item = $this->Tag->order($order)->find();
		}
		else
		{
			$map = array('tag_id' => array('gt', $start));
			$item = $this->Tag->where($map)->order($order)->find();
		}

		if (!empty($item))
		{
		    $QuestionTag = M('QuestionTag');
		    $where = array('tag_id' => $item['tag_id']);
		    $count = $QuestionTag->where($where)->count();
		    $this->Tag->where($where)->setField('question_count', $count);

			$start  = $item['tag_id'];
			$jumUrl = __ACTION__.'/start/'.$start;
			$message = sprintf(L('tag.tag_sync_succeed'), $item['name']);

			$this->assign('jumpUrl', $jumUrl);
			$this->assign('loader', true);
			$this->success($message);
		}
		else
		{
		    $this->redirect('Admin-Tag/tagList');
		}
	}

}
// End  Class

/* End of file TagAction.class.php */
/* Location: ./App/Lib/Action/Admin/TagAction.class.php */